BatchPdfTxt <- function (input_folder, unique_txt){#, output_folder){
#' Create a batch file to convert pdf to text
#' @inputparam input_folder full path of folder that contains pdfs to be converted to txt (character)
#' @inputparam unique_txt part of file names that makes them unique, e.g., "" (character)
####' @output_folder full path of folder for txt files (character)
#' NOTE: pdftotxt must be installed and the bin folder location added to PATH

	## Select all the files to be transformed
		ALL_FILES <- list.files (input_folder)
		DESIRED_FILES_NAMES <- grep (unique_txt, ALL_FILES, value = TRUE)
		DESIRED_FILES_PATHANDNAMES_IN <- paste (input_folder, DESIRED_FILES_NAMES, sep = "/")
		DESIRED_FILES_PATHANDNAMES_OUT <- paste (input_folder, sub("[.]pdf",".txt",DESIRED_FILES_NAMES), sep = "/")
	## Temporary batch file location
		TEMP_BAT_FILE <- paste (getwd(),"pdftotxtTempFile.bat", sep = "/")
	## Write the batch file
		# Make file
		zz <- file(TEMP_BAT_FILE, open = "wt")  # open an output file 
		# Change location
		write(paste("cd",gsub("/","\\\\",input_folder)),zz, append = TRUE)
		# Write pdf to txt extraction
		apply (cbind (DESIRED_FILES_NAMES,sub("[.]pdf",".txt",DESIRED_FILES_NAMES)), 1, function (x) {write(
			paste("C:\\XPDF\\pdftotext ",x[1],gsub("/","\\\\",x[2]))
			, zz,append = TRUE)
		})
		close (zz)
	## Run batch file
		system(shQuote(TEMP_BAT_FILE,type ="cmd"))
	## Remove batch file
		file.remove(TEMP_BAT_FILE)
}

CleanPdfTxt <- function (input., output., NEED_TO_WRITE){
#' Format the raw txt files
#' @inputparam input. full path of txt (character)
#' @inputparam output. full path of txt (character)
#' @inputparam NEED_TO_WRITE write to file or only print (boolean)
	
	## Library
		require(gdata)
	
	## Load
		# Scan file
		TXT_RAW.raw <- scan (input., what = "real")
		
	## Clean
		# Break up lines that aren't broken properly
		TXT_RAW.1 <- unlist(lapply(TXT_RAW.raw , function(x) strsplit(x," ")))
		TXT_RAW.2 <- unlist(lapply(TXT_RAW.1 , function(x) strsplit(x,"\\n"))) # Get rid of returns
		TXT_RAW <- gsub("\\}",")",gsub("\\{","(",TXT_RAW.2)) # Sometimes parenthesis and curly braces get mixed up. This can cause problems later on for splitting, particularly if the first parathensis is given a curly brace.
		# Locate the word reference
		REFERENCE_LOCATION <- tail(grep ("^[r]eference|bibliography|LITERATURE CITED",tolower(TXT_RAW)),1) # Take the last reference in the document in case there are two
		WRITE_OPTION <- "A"
	
	## Do
		if (length (REFERENCE_LOCATION) != 0){
			# Identify date format
			TXT_SELECT <- TXT_RAW[(REFERENCE_LOCATION[1]+1):length(TXT_RAW)]
			TXT_SELECT_DATE.A <- grep ("^[^A-Za-z](19|20)[0-9]{2,2}", TXT_SELECT,value=TRUE)
			TXT_SELECT_DATE.B <- grep ("^([^A-Za-z]|)(19|20)[0-9]{2,2}", TXT_SELECT,value=TRUE)
			if(length(TXT_SELECT_DATE.B)>length(TXT_SELECT_DATE.A)){
				TXT_SELECT_DATE <- grep ("^([^A-Za-z]|)(19|20)[0-9]{2,2}", TXT_SELECT)
				} else {
				TXT_SELECT_DATE <- grep ("^[^A-Za-z](19|20)[0-9]{2,2}", TXT_SELECT)
			}
			TXT_WRITE_ADDITIOANL <- ""
			
			# Check to see that it is not the date in the header/footer
			if (length (TXT_SELECT_DATE) <= 1| length ( unique (	TXT_SELECT[TXT_SELECT_DATE])) == 1){
				TXT_SELECT_DATE <- grep ("(19|20)[0-9]{2,2}[.]", TXT_SELECT)
				TXT_WRITE_ADDITIOANL <- "Second check for date"
			}
			# Convert to string
			TXT_STR <-  gsub("666633339999",",",(gsub(",","",toString(gsub(",","666633339999",TXT_SELECT)))))
			
			if (length (TXT_SELECT_DATE) != 0){
				# Split by date
					TXT_SELECT_DATE_FORMAT <- TXT_SELECT[TXT_SELECT_DATE[1]]
					TXT_SELECT_DATE_FORMAT_GREP <- gsub("\\)","\\\\)",gsub("\\(","\\\\(",gsub("[.]","[.]", sub( "[0-9]{4,4}", "[0-9]{4,4}", TXT_SELECT_DATE_FORMAT))))
					TXT_STR_SPLIT <- matrix(sapply(strsplit (TXT_STR, TXT_SELECT_DATE_FORMAT_GREP)[[1]], function (x) trim (x)),ncol = 1)
					#c(grep (TXT_SELECT_DATE_FORMAT_GREP, TXT_SELECT, value = TRUE),NA)
					
					#if (length (TXT_SELECT[TXT_SELECT_DATE]) == length (TXT_STR_SPLIT[,])){
						TXT_STR_SPLIT_CBIND <- data.frame (DATE = c(NA, grep (TXT_SELECT_DATE_FORMAT_GREP, TXT_SELECT, value = TRUE)),BODY = TXT_STR_SPLIT[,])
						TXT_SELECT_DATE_FORMAT_PASTE <- paste("Date used ( Date format =",TXT_SELECT_DATE_FORMAT_GREP, ")")
					#} else {
					
					#}
					TXT_WRITE <- TXT_STR_SPLIT_CBIND
					OUTPUT <-  paste(TXT_SELECT_DATE_FORMAT_PASTE, TXT_WRITE_ADDITIOANL)
					WRITE_OPTION <- "B"
			} else {		
			
			OUTPUT <- "Reference found, but unable to split by date"
			TXT_WRITE <- TXT_STR
			}
		} else {
			OUTPUT <- "Reference not found. Perform manually."
			TXT_WRITE <- readLines (DESIRED_FILES_NAMES)
		}	
		
		if (NEED_TO_WRITE == TRUE) {
			if (WRITE_OPTION == "A"){
				writeLines (TXT_WRITE,output.)
			}
			
			if (WRITE_OPTION == "B"){
				write.table (TXT_WRITE, output.,row.names = FALSE, sep = "\t")
			}
			} else {
		print (TXT_WRITE)
		}
		print (OUTPUT)
		print (WRITE_OPTION)
		return (OUTPUT)
}